package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.Major;
import com.zzxz.practicaltrainingsys.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/major")
@CrossOrigin
public class MajorController {
    @Autowired
    MajorService majorService;

    @GetMapping("/list")
    public List<Major> list() {
        return majorService.getMajor();
    }

    // 新增专业接口
    @PostMapping("/insert")
    public int insertMajor(@RequestBody Major major) {
        if (major.getMajor_name() == null || major.getMajor_name().trim().isEmpty() || major.getCollege_id() == null) {
            return -1;
        }
        // 可选：查重专业名+学院
        // Major exist = majorService.getMajorByNameAndCollege(major.getMajor_name(), major.getCollege_id());
        // if (exist != null) return 0;
        boolean success = majorService.addMajor(major);
        return success ? 1 : -2;
    }

    //更新
    @PostMapping("/update")
    public int updateMajor (@RequestBody Major major){
        return majorService.updateMajor(major);
    }

    //根据学院编号查找专业名称
    @GetMapping("/findmajor")
    public List<Major> getMajorByCollegeId(@RequestParam Integer college_id) {
        return majorService.getMajorByCollegeId(college_id);
    }


}